package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ChannelDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.pojos.wemedia.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈辉
 * @data 2024 9:39
 */
@RestController
@RequestMapping("/api/v1/channel")
public class WmChannelController {

    @Autowired
    private WmChannelService wmChannelService;

    /**
     * 加载所有频道
     *
     * @return
     */
    @GetMapping("channels")
    @Cacheable(cacheNames = "channel", key="channelList")
    public ResponseResult channels(){
        List<WmChannel> list = wmChannelService.list();
        return ResponseResult.okResult(list);
    }


    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto dto){
        return wmChannelService.findByNameAndPage(dto);
    }

    @PostMapping("/save")
    @CacheEvict(cacheNames = "channel", key="channelList")
    public ResponseResult insert(@RequestBody WmChannel adChannel){
        return wmChannelService.insert(adChannel);
    }

    @PostMapping("/update")
    @CacheEvict(cacheNames = "channel", key="channelList")
    public ResponseResult update(@RequestBody WmChannel adChannel){
        return wmChannelService.update(adChannel);
    }

    @GetMapping("/del/{id}")
    @CacheEvict(cacheNames = "channel", key="channelList")
    public ResponseResult delete(@PathVariable("id") Integer id){
        return wmChannelService.delete(id);
    }
}
